

var globalTree = null;

var UINestable = function() {



    var updateOutput = function(e) {

        var list = e.length ? e : $(e.target),
                output = list.data('output');

        if (window.JSON) {
            globalTree = window.JSON.stringify(list.nestable('serialize'));
            output.val(window.JSON.stringify(list.nestable('serialize'))); //, null, 2));

            //
//
//
            //globalTr
            //alert(window.JSON.stringify(list.nestable('serialize')));
        } else {
            output.val('JSON browser support required for this demo.');
        }
    };


    return {
        //main function to initiate the module
        init: function() {

            // activate Nestable for list 2
//            $('#treeview').nestable({
//                group: 1
//            })
//                    .on('change', updateOutput);

            $('#treeview').nestable({
                group: 1
            })
                    .on('change', updateOutput);

            // e.stopPropagation();

//                var id = $(this).data('id'),
//                        parentId = $(this).parents('.dd-item').data('id');
//                alert(parentId);

            // output initial serialised data


            updateOutput($('#treeview').data('output', $('#treeview_output')));
            //  updateOutput(alert("mensaje"));


            $('#nestable_list_menu').on('click', function(e) {
                var target = $(e.target),
                        action = target.data('action');
                if (action === 'expand-all') {
                    $('.dd').nestable('expandAll');
                }
                if (action === 'collapse-all') {
                    $('.dd').nestable('collapseAll');
                }
            });


            $("#btntreeview").click(function() {


                var url = "/GedeonMvc/rol/treeview";

                $.ajax(
                        {
                            type: "POST",
                            //dataType: "json",
                            url: url,
                            data: {treeview: globalTree},
                            success: function(response)
                            {
                                // alert(response);
                                
                                $("#alertload").html(response);
                               // $( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
                            //    $("#divmessage").replaceWith(response);

                            },
                            error: function(error)
                            {
                                alert("error:" + error);
                            }
                        });
            });
            // $('#treeview').nestable();
        }
    };

}();
